//
//  HomePageViewController.m
//  HeWeiZhiNeng
//
//  Created by chengzhang Yan on 2017/11/30.
//  Copyright © 2017年 YanCeyMichael.github.io. All rights reserved.
//

#import "HomePageViewController.h"
#import "CarDetailViewController.h"
#import "HomePageControl.h"
#import "HomeHeaderView.h"
#import "HomeScrollView.h"

@interface HomePageViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong)HomePageControl *pageC;
@property (nonatomic, strong)HomeHeaderView *headerView;
@property (nonatomic, strong)HomeScrollView *homeView;
@property (nonatomic, strong)hImageTextView *yuyinView;
@property (nonatomic, strong)LoginHeadView *lHeadView;
@end

@implementation HomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = Color.back;
    self.navigationController.navigationBar.hidden = YES;
    [self.view addSubview:self.headerView];
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top);
        make.leading.and.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(145);
    }];
    [self.view addSubview:self.homeView];
    [self.view addSubview:self.pageC];
    self.homeView.isLogin = YES;
    [self.view addSubview:self.lHeadView];
    self.lHeadView.headImageView.image = [UIImage imageNamed:@"sy_chetoxiang_img"];
    self.lHeadView.hidden = NO;
    [self.yuyinView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view.mas_bottom);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(100, 88));
    }];
    
    UITapGestureRecognizer *carTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(goToCarDetailAction)];
    [self.lHeadView addGestureRecognizer:carTap];
    // Do any additional setup after loading the view.
}

#pragma mark ----- 创建滚动视图 -----
-(HomeScrollView *)homeView{
    if (!_homeView) {
        _homeView = [[HomeScrollView alloc]initWithFrame:CGRectMake(15, 90, kScreenW-30, 324)];
        _homeView.delegate = self;
    }
    return _homeView;
}

-(HomePageControl *)pageC{
    if (!_pageC) {
        _pageC = [[HomePageControl alloc]initWithFrame:CGRectMake(kScreenW/2-20, 90+324+21, 40, 20)];
        [_pageC addTarget:self action:@selector(page:) forControlEvents:UIControlEventValueChanged];
    }
    return _pageC;
}

#pragma mark ----- 创建头部视图 -----
-(HomeHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[HomeHeaderView alloc]init];
    }
    return _headerView;
}

#pragma mark ----- 创建头像视图 -----
-(LoginHeadView *)lHeadView{
    if (!_lHeadView) {
        _lHeadView = [[LoginHeadView alloc]initWithFrame:CGRectMake(kScreenW/2-45, 45, 90, 90)];
    }
    return _lHeadView;
}

#pragma mark ----- 语音 -----
-(hImageTextView *)yuyinView{
    if (!_yuyinView) {
        _yuyinView = [[hImageTextView alloc]init];
        _yuyinView.titleLabel.text = @"按住语音控制~";
        _yuyinView.titleView.image = [UIImage imageNamed:@"sy_yuyin"];
        [self.view addSubview:_yuyinView];
    }
    return _yuyinView;
}

#pragma mark ----- 跳转车辆详情 -----
-(void)goToCarDetailAction{
    CarDetailViewController *carVC = [[CarDetailViewController alloc]init];
    [self.navigationController pushViewController:carVC animated:YES];
}

#pragma mark ----- 滚动视图联动 -----
-(void)page:(UIPageControl *)page{
    [self.homeView setContentOffset:CGPointMake(page.currentPage * (kScreenW -30), 0) animated:YES];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGPoint p = self.homeView.contentOffset;
    NSInteger page = p.x/(kScreenW-30);
    self.pageC.currentPage = page;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

@implementation hImageTextView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.mas_top);
            make.centerX.mas_equalTo(self.mas_centerX);
            make.size.mas_equalTo(CGSizeMake(40, 40));
        }];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(18);
            make.leading.mas_equalTo(self.mas_leading);
            make.trailing.mas_equalTo(self.mas_trailing);
            make.bottom.mas_equalTo(self.mas_bottom).offset(-20);
        }];
    }
    return self;
}

#pragma mark ----- 开发中图片 -----
-(UIImageView *)titleView{
    if (!_titleView) {
        _titleView = [[UIImageView alloc]init];
        [self addSubview:_titleView];
    }
    return _titleView;
}

#pragma mark ----- 内容标签----
-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        [self addSubview:_titleLabel];
        [self CreatNewLabel:_titleLabel color:Color.stub font:Common_Font(12)];
    }
    return _titleLabel;
}

-(void)CreatNewLabel:(UILabel *)label color:(UIColor *)color font :(UIFont *)font{
    label.userInteractionEnabled = YES;
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = color;
    label.font = font;
}
@end

@implementation LoginHeadView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 45;
        self.layer.masksToBounds = YES;
        [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.mas_centerY);
            make.centerX.mas_equalTo(self.mas_centerX);
            make.size.mas_equalTo(CGSizeMake(80, 80));
        }];
    }
    return self;
}

#pragma mark ----- 开发中图片 -----
-(UIImageView *)headImageView{
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc]init];
        _headImageView.layer.cornerRadius = 40;
        _headImageView.layer.masksToBounds = YES;
        [self addSubview:_headImageView];
    }
    return _headImageView;
}
@end
